package cn.ainit.sys.pages.config;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.sys.menu.SysFunctions;
import cn.ainit.sys.pages.config.entity.ConfigFunctions;
import cn.ainit.sys.pages.config.entity.ConfigObject;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.QueryJson;
import cn.ainit.sys.ui.controller.UIService;

@Controller
@RequestMapping("/pageConfig")
public class PagesConfigController {
	private PagesConfigService pagesConfigService;
	private UIService uiService;
	 
	@Resource
	public void setUiService(UIService uiService) {
		this.uiService = uiService;
	}
	@Resource
	public void setPagesConfigService(PagesConfigService pagesConfigService) {
		this.pagesConfigService = pagesConfigService;
	}
 
	@RequestMapping("/pagesUrl")
	public String pagesUrl(String configId,String funcsId, HttpServletRequest request) throws Exception{
		//1.根据congigId 去sys_config_object中查找这个实体类（得到各种需要的Id）typeId为：queryPage查询  mainList主明细
		request.setAttribute("configId",configId);
		request.setAttribute("funcsId",funcsId);
		ConfigObject configObject = this.pagesConfigService.getOneConfigObject(configId);
		if (configObject.getTypeId().equals("queryPage")) {  //为查询界面
			return "homepages/query/querycol";
		}else if(configObject.getTypeId().equals("mainList")) { //为主明细界面
			return "homepages/mainlist/mainList2";
		}
		return "error";
	}
	
	/***
	 * 生成各种界面
	 * @param configId
	 * @param funcsId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping("/createPages")
	@ResponseBody
	public QueryJson createPages(String configId,String funcsId, HttpServletRequest request) throws Exception{
		try {
			QueryJson queryJson = new QueryJson();
			//1.根据congigId 去sys_config_object中查找这个实体类（得到各种需要的Id）typeId为：queryPage查询  mainList主明细
			ConfigObject configObject = this.pagesConfigService.getOneConfigObject(configId);
			//2.根据funcsId 去sys_config_function中查找这个实体类（得到相应的方法）
			ConfigFunctions configFunctions = this.pagesConfigService.getOneConfigFunctions(funcsId);   //暂时还没有用到
			DataStore dataStore  = new DataStore();
			//3  根据object中的typeId 给QueryJson中的各个变量赋值
			if (configObject.getTypeId().equals("queryPage")) {  //为查询界面
				queryJson.setColumns(this.uiService.queryColumns(configObject.getQueryColDsId()));	//query datagrid
				queryJson.setQueryColumns(this.pagesConfigService.queryOneColumns(configObject.getQueryColDsId()));  //query 左上			
				queryJson.setButtons(this.uiService.getQueryButns(configObject.getQueryButnId()));   //query  按钮
				//添加Rows
				dataStore= this.uiService.getDataStoreById(configObject.getQueryColDsId());
				
			}else if(configObject.getTypeId().equals("mainList")) { //为主明细界面
				queryJson.setColumns(this.uiService.getColumnsById(configObject.getUiColDsId())); //左上边的columns   ui
				queryJson.setQueryColumns(this.uiService.queryColumns(configObject.getQueryColDsId()));	//datagrid中的column  query
				queryJson.setButtons(this.uiService.findAllButtons(configObject.getUiButnId()));  //按钮   UI
				//添加Rows
				dataStore = this.uiService.getDataStoreById(configObject.getUiColDsId());
			}
			List<Object> list = (List<Object>) this.uiService.queryColBySql(dataStore.getSqlText());
			queryJson.setRows(list);
			queryJson.setPageNumber(1);
			queryJson.setPageSize(10);
			//4.最后返回这个QueryJson
			return queryJson;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/saveConfig")
	@ResponseBody
	public int addObject(ConfigObject configObj,HttpServletRequest request) throws Exception{
		int i=0;
		try {
			if(this.pagesConfigService.saveConfigObject(configObj)){
				i=1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
	@RequestMapping("/saveFunc")
	@ResponseBody
	public int saveFunc(String grade,SysFunctions sysFunctions,HttpServletRequest request) throws Exception{
		int i=0;
		try {
			if (grade.equals("two")) {
				sysFunctions.setRootId(sysFunctions.getSuperFuncId());
			}
			if (grade.equals("san")){
				//根据父Id去查找当条数据的RootID
				SysFunctions function = this.pagesConfigService.getOneFunction(sysFunctions.getSuperFuncId());
				sysFunctions.setRootId(function.getRootId());
			}
			if(this.pagesConfigService.saveFunc(sysFunctions)){
				i=1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
}
